using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;

namespace ArchSoft
{
    public class InsertTitleBlock2MainViewportFile
    {
        string _sourcePath;
        string Szfile;
        public InsertTitleBlock2MainViewportFile(string source,string SzFl)
        {
            _sourcePath = source;
            Szfile = SzFl;
        }

        public bool DeleteTarget()
        {
            // Delete .bak file
            try
            {
                DirectoryInfo source = new DirectoryInfo(_sourcePath);
                foreach (DirectoryInfo subDir in source.GetDirectories())
                {
                    foreach (FileInfo file in subDir.GetFiles())
                    {
                        if (!file.Extension.Equals(".dwg") && !file.Extension.Equals(".dwt"))
                        {
                            file.Delete();
                        }
                    }
                }
                return true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; }
        }

        private void DeleteTempFile()
        {
            DirectoryInfo issueWindow = new DirectoryInfo(_sourcePath);
            foreach (FileInfo adrawing in issueWindow.GetFiles())
            {
                    adrawing.Delete();                
            }

            foreach (DirectoryInfo dir in issueWindow.GetDirectories())
            {                
                if (dir.Name.ToLower().Contains("reference"))
                {
                    dir.Delete(true);
                }
                else
                {
                    foreach (FileInfo adrawing in dir.GetFiles())
                    {
                        if (!adrawing.Extension.Contains("dwg") && !adrawing.Extension.Contains("dwt"))
                        {
                            adrawing.Delete();
                        }
                    }
                }
            }
        }

        public string GenerateScriptFile(string childWindow, List<string> drawings)
        {
            if (!File.Exists(_sourcePath + "\\Xref\\A_Title Block.dwg"))
            {
                MessageBox.Show("Can not find " + _sourcePath + "\\Xref\\A_Title Block.dwg.", "Missing file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return "";
            }

            // Generate script
            string tempFileName = _sourcePath + @"\" + Guid.NewGuid().ToString() + ".scr";

            FileStream scriptFile = File.Open(tempFileName, FileMode.CreateNew);
            StreamWriter wr = new StreamWriter(scriptFile);

            wr.WriteLine("filedia 0");

            ////Design Viewport;
            wr.WriteLine("open \"" + _sourcePath + "\\Xref\\A_Title Block.dwg" + "\"");
            wr.WriteLine("TILEMODE");
            wr.WriteLine("1");
            wr.WriteLine("-INSERT");
            wr.WriteLine("\"" + Szfile + "\"");//Attention Path of Viewport Drawing
            wr.WriteLine("0,0");
            wr.WriteLine("");//Drawing Scale
            wr.WriteLine("");//Drawing Scale
            wr.WriteLine("");
            wr.WriteLine("EXPLODE");
            wr.WriteLine("LAST");
            wr.WriteLine("");
            wr.WriteLine("");
            wr.WriteLine("-PURGE");
            wr.WriteLine("A");
            wr.WriteLine("*");
            wr.WriteLine("N");       
            wr.WriteLine("QSAVE");
            //wr.WriteLine("Close");

            wr.WriteLine("filedia 1");
            wr.WriteLine("quit");
            wr.Flush();
            wr.Dispose();

            scriptFile.Close();
            scriptFile.Dispose();
            return tempFileName;
        }    

        public string RunSciptFile(string filePath)
        {
            AutocadHelper helper = AutocadHelper.Instance;
            return helper.RunAutoCadScript(filePath, true);        
        }

        public string Process(string childWindow, List<string> drawings)
        {
            string str = "";

           bool tpd = DeleteTarget(); if (!tpd) { return ""; }            
            string scriptFile = GenerateScriptFile(childWindow, drawings);
            if (scriptFile != "")
                str = RunSciptFile(scriptFile);
            DeleteTempFile();
            return str;
        }             

        public string Process()
        {
            return Process(string.Empty, null);
        }        
    }
}
